﻿@model EvincePayroll.Data.EmployeeLeaveRequestData
@{
    ViewBag.Title = "LeaveRequestsEmployee";
    var temp = ViewBag.LeaveType as System.Collections.IEnumerable;
}
<input type="hidden" id="sickGuid" value="@ViewBag.Sick" />
<input type="hidden" id="cLGuid" value="@ViewBag.CL" />
<input type="hidden" id="pLGuid" value="@ViewBag.PL" />
<input type="hidden" id="lWPGuid" value="@ViewBag.LWP" />
@*<link href="../../Content/datetimepicker.css" rel="stylesheet" type="text/css" />

<script src="../../js/jquery-ui/jquery.ui.datepicker.min.js" type="text/javascript"></script>*@



<script type="text/javascript">
    var sick = $('#sickGuid').val();
    var PL = $('#pLGuid').val();
    var CL = $('#cLGuid').val();
    var LWP = $('#lWPGuid').val();
$(document).ready(function () {
    $('#CurrentLeavesInAccount').load('/LeaveRequests/_CurrentLeavesInAccount');
    setDatePickerFrom('date-picker-from');
    setDatePickerFrom('date-picker-to');
    //  var leaveType = $('#leaveType').val();
    $('#leaveTypeFrom').hide();
    $('#leaveTypeTo').hide();
    $('#halfday').click(function () {
        $('#leaveTypeFrom').show();
        $('#leaveTypeTo').hide();
    });
    $('#fullday').click(function () {
        $('#leaveTypeFrom').show();
        $('#leaveTypeTo').show();
    });





    $('#reasonerror').hide();
    $('#fromDate').hide();
    $("#leaveTypeError").hide();
    $('#todate').hide();
    $('#reportToError').hide();
    $('#btnCancel').click(function () {
        $('#txtLeaveReason').val('');
        $('#date-picker-to').val('');
        $('#date-picker-from').val('');
        $('#fullday').val('');
        $('#halfday').val('');
        return false;
    });

    $("#validate1").submit(function () {
        var errorFlag = false;
        $('.requiredField').each(function () {
            if ($(this).val() == '') {
                if ($(this).prev().attr('class') != 'error') {
                    $(this).after('<span class="error"></span>');
                }
                $(this).css({
                    border: '1px solid red'
                });
                errorFlag = true;
            } else {
                if ($(this).prev().attr('class') == 'error') {
                    $(this).prev().remove();
                    $(this).removeAttr('style');
                }
            }
        });
        //var checkLeaves = false;
        
        if (errorFlag == true) {

            return false;
        }
        return calculateDays();
    });


});

//    function CheckLeave() {
//        var LeaveType = ('#leaveStatusType').val();
//        $.ajax({
//            type: "POST",
//            url: "/LeaveRequests/CheckLeave?leaveType=" + LeaveType,
//            success: function (result) {
//            
//                
//                return false;
//            }
//        });




//  }
function calculateDays() {
    var flag = false;
    var leaveStatusType = $('#leaveStatusType').val();
    if (leaveStatusType != '') {
        var sickLeave = $('#sickLeave').text();
        var Casualleave = $('#Casualleave').text();
        var PrivilageLeave = $('#PrivilageLeave').text();
        var LeaveWithoutPay = $('#LeaveWithoutPay').text();
        var FromDate = $('#date-picker-from').val();
        var TODate = $('#date-picker-to').val();
        var from = new Date(FromDate);
        var to = new Date(TODate);
        var days = daydiff(from, to) + 1;

        if (leaveStatusType == sick) {

            if (days > 0) {
                if (sickLeave >= days) {
                    flag = true;
                }
            }
        }
        if (leaveStatusType == CL) {
            if (days > 0) {
                if (Casualleave >= days) {
                    flag = true;
                }
            }
        }
        if (leaveStatusType == PL) {
            if (days > 0) {
                if (PrivilageLeave >= days) {
                    flag = true;
                }
            }
        }
        if (leaveStatusType == LWP) {
            if (days > 0) {
                if (LeaveWithoutPay >= days) {
                    flag = true;
                }
            }
        }
    }
    if (flag == false) {
        alert("You do not have sufficient leave in your account!");
    }
    return flag;
    
}
function daydiff(first, second) {
    return (second - first) / (1000 * 60 * 60 * 24)
}
    </script>







@using (@Html.BeginForm("LeaveRequestsEmployee", "LeaveRequests", FormMethod.Post, new { id = "validate1" }))
{
    <div class="grid_5">

            <div class="box round">
                <h2>Request</h2>
                <div class="block">
                    <div id="chart1">
                    <div id="CurrentLeavesInAccount"></div>


                    <table class="form">
                        <tr>
                            <td align="center">
                                <label class ="center">
                                    Leave Reason</label>
                            </td>
                            <td>
                            @Html.TextBoxFor(m => m.LeaveReason, new { style = "width:50%", id = "txtLeaveReason",@class="requiredField" })
                                
                               <span class="error" id="reasonerror">*</span>
                            </td>
                            </tr>
                             <tr>
                            <td align="center">
                                <label class ="center">
                                   Leave Type</label>
                            </td>
                            <td>
                             @Html.DropDownListFor(m => m.LeaveStatusType, new SelectList(ViewBag.LeaveType as System.Collections.IEnumerable, "ParamId", "ParamValue"), "", new { style = "width:50%", id = "leaveStatusType", @class = "requiredField" })
                                
                               
                            </td>
                            </tr>
                            <tr>
                            <td align="center">
                               
                            </td>
                            <td>
                            <input type="radio" id="halfday" name="option" /><label for="halfday">Half Day</label>
                            <input type="radio" id="fullday" name="option" /><label for="fullday">Full Day</label>
                           
                                
                                <span class="error" id="leaveTypeError">*</span>
                            </td>
                            </tr>
                            <tr id="leaveTypeFrom">
                            <td align="center">
                                <label>
                                    From</label>
                            </td>
                            <td>
                                 @Html.TextBoxFor(m => m.FromDate , new { style = "width:50%", id = "date-picker-from", @readonly = "readonly", @class = "requiredField" })
                                 @*<input id="date-picker-from" name="FromDate" type="text" readonly="readonly" />*@

                                <span class="error" id="fromDate">*</span>
                            </td>
                            </tr>
                            <tr id="leaveTypeTo">
                            <td align="center">
                                <label>
                                    To</label>
                            </td>
                            <td>
                                 @Html.TextBoxFor(m => m.ToDate, new { style = "width:50%", id = "date-picker-to", @readonly = "readonly" })
                                 @*<input id="date-picker-to" name="ToDate" type="text" readonly="readonly" />*@
                                <span class="error" id="todate">*</span>
                            </td>
                            </tr>
                         <tr>
                            <td align="center">
                                <label>
                                    Report To</label>
                            </td>
                            <td>
                                 @Html.DropDownListFor(m => m.ReportTo, new SelectList(ViewBag.ReportTo as System.Collections.IEnumerable, "Id", "ReportToManager"), "", new { style = "width:50%", id = "ReportTo", @class = "requiredField" })
                                <span class="error" id="reportToError">*</span>
                            </td>
                           
                        </tr>
                        <tr>
                            <td align="center">
                                <label>
                                    Assign To</label>
                            </td>
                            <td>
                               @Html.DropDownListFor(m => m.AssignTo, new SelectList(ViewBag.AssignTo as System.Collections.IEnumerable, "Id", "AssignToEmployee"), "", new { style = "width:50%", id = "AssignTo", })
                               @* <span class="error">This is a required field.</span>*@
                            </td>
                           
                        </tr>
                        <tr>
                            <td align="center">
                                <div class="block">
                                @*<input type="button" runat="server" class="btn btn-orange" value="Login" />*@
                       <button class="btn btn-orange">Submit</button>
                       </div>
                            </td>
                            <td>
                                 <div class="block">
                       <button class="btn btn-orange" id="btnCancel">Cancel</button>
                       </div>
                            </td>
                        </tr>
                    </table>
                   

                    </div>
                </div>
            </div>
            
        </div>
}